﻿/*======================================================================================================================================================

----------------KHAI BÁO BIẾN TOÀN CỤC LƯU THÔNG TIN PAGE

======================================================================================================================================================*/
var defaultName = "oos-video-preview";
var pageLink;
var previousTop = 0;

var videoId;
var videoStyleOld;
var videoStyleNew;
var videoWidth;
var videoHeight;

var parentId;
var parentClass;
var parentStyleNew;
var parentStyleOld;

var isTransformed;
var yPosition;

/*======================================================================================================================================================

----------------KHAI BÁO HÀM NHẬN MESSAGE

======================================================================================================================================================*/
if (document.getElementById("p-data-temp") == null) {
    var para = document.createElement("p");
    para.id = "p-data-temp";
    para.style = "visibility: hidden;";
    var t = document.createTextNode("1");
    para.appendChild(t);
    document.body.appendChild(para);
}

self.port.on("play-video", function () {
    document.getElementById(videoId).play();
});

self.port.on("pause-video", function () {
    document.getElementById(videoId).pause();
});

self.port.on("stop-video", function () {
    document.getElementById(videoId).stop();
});

self.port.on("analize-page", function (link) {
    /*
        Việc phân tích trang hiện tại hỗ trợ một số trang chủ yếu với kết cấu cố định, tag cố định
        Hàm thực hiện tìm video vì một số nguyên nhân khiến nó khó có thể thực hiện được vi mỗi trang có một kết cấu khác nhau
    */
    isTransformed = false;

    if (document.getElementById("p-data-temp") == null) {
        var para = document.createElement("p");
        para.id = "p-data-temp";
        para.style = "visibility: hidden;";
        var t = document.createTextNode("1");
        para.appendChild(t);
        document.body.appendChild(para);
    }

    if (link.search("https://www.youtube.com/") == 0) {
        pageLink = "https://www.youtube.com/";
        videoId = "player-api";
        videoStyleNew = "cursor:pointer; margin-top: -10px; border: 8px solid white; box-shadow: 0px 0px 8px black; width: 321px; height:180px; z-index: 999999; position: fixed; transform: none; border-radius: 0px 0px 6px 6px;";
        videoStyleOld = "overflow: hidden;";
        parentId = "player-mole-container";
        parentStyleOld = "";
        parentStyleNew = "height: 500px;";
        yPosition = 390;

    } else if (link.search("http://tv.zing.vn/") == 0) {
        pageLink = "http://tv.zing.vn/";
        videoId = "BaseProject";
        videoHeight = document.getElementById(videoId).getAttribute("height");
        videoWidth = document.getElementById(videoId).getAttribute("width");

        parentClass = "_insideBackground";
        parentStyleNew = "position: fixed; margin-top: -95px; margin-left: 600px; float: left; cursor:pointer; border: 8px solid white; box-shadow: 0px 0px 8px black; z-index: 999999; border-radius: 0px 0px 6px 6px;";
        yPosition = document.getElementById(videoId).getBoundingClientRect().bottom;
    } else if (link.search("http://dantri.com.vn/") == 0) {
        pageLink = "http://dantri.com.vn/";
        videoId = "object_dantri";
        var allEmbedItem = document.getElementsByTagName("embed");
        for (var i = 0; i < allEmbedItem.length; i++)
            if (allEmbedItem[i].id == videoId) {
                videoId = defaultName;
                allEmbedItem[i].id = videoId;
                break;
            }
        videoHeight = document.getElementById(videoId).getAttribute("height");
        videoWidth = document.getElementById(videoId).getAttribute("width");
        videoStyleOld = document.getElementById(videoId).getAttribute("style");
        videoStyleNew = "cursor:pointer; margin-top: -242px; margin-left: -145px; float: top; border: 8px solid white; box-shadow: 0px 0px 8px black; z-index: 999999; position: fixed; transform: none; border-radius: 0px 0px 6px 6px;";
        yPosition = document.getElementById(videoId).getBoundingClientRect().bottom;
        console.log(">> yPosition: " + yPosition);
    } else if (link.search("http://phimvang.org/") == 0) {
        pageLink = "http://phimvang.org/";
        videoId = "mediaspace";
        videoHeight = document.getElementById(videoId).getAttribute("height");
        videoWidth = document.getElementById(videoId).getAttribute("width");
        videoStyleOld = document.getElementById(videoId).getAttribute("style");
        videoStyleNew = "cursor:pointer; float: left; margin-left: 696px; margin-top: -355px; border: 8px solid white; box-shadow: 0px 0px 8px black; width: 321px; height:180px; z-index: 999999; position: fixed; transform: none; border-radius: 0px 0px 6px 6px;";
        yPosition = document.getElementById(videoId).getBoundingClientRect().bottom;
    } else if (link.search("http://phim47.com/") == 0) {
        pageLink = "http://phim47.com/";
        videoId = "player_view";
        videoStyleOld = "";
        videoStyleNew = "z-index: 1000; border: 8px solid rgb(255, 255, 255); box-shadow: 0px 0px 8px black; border-radius: 0px 0px 6px 6px; width: 321px; height: 180px; background: none repeat scroll 0% 0% black; float: right; margin-right: 345px; margin-top: 0px; position: fixed;";
        yPosition = 500;
    }






    //======================================================================================================================================
});

self.port.on("accept-site", function () {
    if (document.getElementById("p-data-temp") != null) {
      document.getElementById("p-data-temp").innerHTML = 1;
    }
});

self.port.on("except-site", function () {
    if (document.getElementById("p-data-temp") != null) {
        document.getElementById("p-data-temp").innerHTML = 0;
        switch (pageLink) {
            case "https://www.youtube.com/":
                document.getElementById(videoId).setAttribute("style", videoStyleOld);
                document.getElementById(parentId).setAttribute("style", parentStyleOld);
                break;
            case "http://tv.zing.vn/":
                document.getElementById(videoId).setAttribute("width", videoWidth);
                document.getElementById(videoId).setAttribute("height", videoHeight);

                document.getElementsByClassName(parentClass)[0].setAttribute("style", "");
                break;
            case "http://dantri.com.vn/":
                document.getElementById(videoId).setAttribute("width", videoWidth);
                document.getElementById(videoId).setAttribute("height", videoHeight);
                document.getElementById(videoId).setAttribute("style", videoStyleOld);
                break;
            case "http://phimvang.org/":
                document.getElementById(videoId).setAttribute("width", videoWidth);
                document.getElementById(videoId).setAttribute("height", videoHeight);
                document.getElementById(videoId).setAttribute("style", videoStyleOld);
                break;
            case "http://phim47.com/":
                document.getElementById(videoId).setAttribute("style", videoStyleOld);
                break;




                //======================================================================================================================================
        }
    }
});

/*======================================================================================================================================================

----------------KHAI BÁO HÀM

======================================================================================================================================================*/

window.onscroll = function (event) {
    if (document.getElementById("p-data-temp") != null) {

        var previewMode =document.getElementById("p-data-temp").innerHTML;
        if (previewMode == 1) {
            var $h1 = $("body");
            var window_offset = $h1.offset().top - $(window).scrollTop();

            if (window_offset - previousTop <= 0) {
                // Xử lý cuộn xuống
                previousTop = window_offset;
                if (isTransformed == false && window_offset < -yPosition)
                    switch (pageLink) {
                        case "https://www.youtube.com/":
                            document.getElementById(videoId).setAttribute("style", videoStyleNew);
                            document.getElementById(parentId).setAttribute("style", parentStyleNew);
                            isTransformed = true;
                            break;
                        case "http://tv.zing.vn/":
                            document.getElementById(videoId).setAttribute("width", "321");
                            document.getElementById(videoId).setAttribute("height", "180");

                            document.getElementsByClassName(parentClass)[0].setAttribute("style", parentStyleNew);
                            isTransformed = true;
                            break;
                        case "http://dantri.com.vn/":
                            document.getElementById(videoId).setAttribute("width", "321");
                            document.getElementById(videoId).setAttribute("height", "180");
                            document.getElementById(videoId).setAttribute("style", videoStyleNew);
                            isTransformed = true;
                            break;
                        case "http://phimvang.org/":
                            document.getElementById(videoId).setAttribute("width", "");
                            document.getElementById(videoId).setAttribute("height", "");
                            document.getElementById(videoId).setAttribute("style", videoStyleNew);
                            isTransformed = true;
                            break;
                        case "http://phim47.com/":
                            document.getElementById(videoId).setAttribute("style", videoStyleNew);
                            break;



                            //======================================================================================================================================


                    }
            }
            else {
                // xử lý cuộn lên
                previousTop = window_offset;
                if (isTransformed == true && window_offset > -yPosition)
                    switch (pageLink) {
                        case "https://www.youtube.com/":
                            document.getElementById(videoId).setAttribute("style", videoStyleOld);
                            document.getElementById(parentId).setAttribute("style", parentStyleOld);
                            isTransformed = false;
                            break;
                        case "http://tv.zing.vn/":
                            document.getElementById(videoId).setAttribute("width", videoWidth);
                            document.getElementById(videoId).setAttribute("height", videoHeight);

                            document.getElementsByClassName(parentClass)[0].setAttribute("style", "");
                            isTransformed = false;
                        case "http://dantri.com.vn/":
                            document.getElementById(videoId).setAttribute("width", videoWidth);
                            document.getElementById(videoId).setAttribute("height", videoHeight);
                            document.getElementById(videoId).setAttribute("style", videoStyleOld);
                            isTransformed = false;
                            break;
                        case "http://phimvang.org/":
                            document.getElementById(videoId).setAttribute("width", videoWidth);
                            document.getElementById(videoId).setAttribute("height", videoHeight);
                            document.getElementById(videoId).setAttribute("style", videoStyleOld);
                            break;
                        case "http://phim47.com/":
                            document.getElementById(videoId).setAttribute("style", videoStyleOld);
                            break;



                            //======================================================================================================================================

                    }
            }
        }
        else if (isTransformed == true)
            switch (pageLink) {
                case "https://www.youtube.com/":
                    document.getElementById(videoId).setAttribute("style", videoStyleOld);
                    document.getElementById(parentId).setAttribute("style", parentStyleOld);
                    isTransformed = false;
                    break;
                case "http://tv.zing.vn/":
                    document.getElementById(videoId).setAttribute("width", videoWidth);
                    document.getElementById(videoId).setAttribute("height", videoHeight);

                    document.getElementsByClassName(parentClass)[0].setAttribute("style", "");
                    isTransformed = false;
                    break;
                case "http://dantri.com.vn/":
                    document.getElementById(videoId).setAttribute("width", videoWidth);
                    document.getElementById(videoId).setAttribute("height", videoHeight);
                    document.getElementById(videoId).setAttribute("style", videoStyleOld);
                    isTransformed = false;
                    break;
                case "http://phimvang.org/":
                    document.getElementById(videoId).setAttribute("width", videoWidth);
                    document.getElementById(videoId).setAttribute("height", videoHeight);
                    document.getElementById(videoId).setAttribute("style", videoStyleOld);
                    break;
                case "http://phim47.com/":
                    document.getElementById(videoId).setAttribute("style", videoStyleOld);
                    break;



                    //======================================================================================================================================

            }
    }
}